package com.eric.rabbitmq.simpe;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    //队列名称
    public static  final  String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //工厂IP 连接mq队列
        factory.setHost("172.100.2.13");
        //用户名和密码
        factory.setUsername("admin");
        factory.setPassword("admin");
        //创建连接
        Connection connection = factory.newConnection();
        //获取信道
        Channel channel = connection.createChannel();
        //声明，接收消息
        DeliverCallback deliverCallback = (consumerTag,message)->{
            System.out.println(message);
        };
        CancelCallback cancelCallback = consumerTag->{
          System.out.println("消息消费被中断");
        };
        //接收消息
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);
    }
}
